(:
   Convert an "elan" transcription to a display file
   JJG / 2008-03-14 / A very first version...
 :)
declare boundary-space strip;
declare variable $fid := $arguments[1] ;
declare function local:build-Path($elt as element()) as xs:string
{
   if (fn:empty($elt/..))
   then
      ""
   else
      let $n := fn:local-name($elt)
      for $i at $j in $elt/../*[fn:local-name(.)=$n]
      where $i is $elt
      return fn:concat(local:build-Path($elt/..), "/", $n, "[", $j, "]")
 
};
declare variable $d := doc($fid) ;
declare variable $ts := $d/ANNOTATION_DOCUMENT/TIME_ORDER/TIME_SLOT ;
declare variable $ann := $d/ANNOTATION_DOCUMENT/TIER/ANNOTATION ;
declare function local:ts($s as xs:string) as xs:long
{
   xs:long($ts[@TIME_SLOT_ID = $s]/@TIME_VALUE)
}
(: Generate the events :)
<display>{
    for $it in $ann
    let $us := xs:string($it/../@TIER_ID)
    let $beg := local:ts(xs:string($it/ALIGNABLE_ANNOTATION/@TIME_SLOT_REF1))
    let $end := local:ts(xs:string($it/ALIGNABLE_ANNOTATION/@TIME_SLOT_REF2))
    let $mytext := $it/ALIGNABLE_ANNOTATION/ANNOTATION_VALUE/text()
    order by $beg
    return
if ($mytext) then
    <item>{
          <info name="user">{$us}</info>,
          <info name="software">elan</info>,
          <info name="tool">transcript</info>,
          <info name="type">transcript</info>,
          <info name="time"><time>{<date>{$beg}</date>,
              if ($end > $beg)
                 then <duration>{$end - $beg}</duration>
                 else ()
          }</time></info>,
          <info name="src-anchor"><anchor>{
              <doc>{$fid}</doc>,
              <path>{local:build-Path($it)}</path>
          }</anchor></info>,
          <info name="content"><text>{xs:string($mytext)}</text></info>
    }</item>
else ()
}</display>

